﻿CREATE PROCEDURE [dbo].[GetPageAttachmentByAttachmentTypeAndDownloadStatus]	
	@attachmentType NVARCHAR (255), 
	@downloadStatus INT
AS
BEGIN
	BEGIN TRANSACTION 
	DECLARE @result table(
		[ID]					BIGINT				NOT NULL,        
		[PageID]				BIGINT				NULL,
		[PullTask]				NVARCHAR (255)		NULL,
		[AttachmentType]		NVARCHAR (255)		NOT NULL,
		[PageAttachmentUrl]		NVARCHAR(1024)		NOT NULL,
		[StoredPath]			NVARCHAR (MAX)		NOT NULL,
		[Downloaded]			INT					NOT NULL DEFAULT 0, --0:Created, 1:downloaed: 2:downloading
		[PullDate]				DATETIME
		)
	INSERT INTO @result 
	SELECT TOP 100 * FROM PageAttachment WITH(UPDLOCK) where AttachmentType = @attachmentType and Downloaded = @downloadStatus
	
	UPDATE PageAttachment SET Downloaded = 2 WHERE ID IN (SELECT ID FROM @result)
	
	SELECT * FROM @result 	
	
	COMMIT TRANSACTION 	
END